<?php
    if ($collection->type === 'feed') {
        $feed = [
            'type' => 'application/atom+xml',
            'url' => $collection->feed_url,
            'title' => _f('Syndication feed of %s', $collection->name),
        ];

        $current_tab = 'feeds';
    } else {
        $feed = [
            'type' => 'application/atom+xml',
            'url' => url_full('collection feed', ['id' => $collection->id]),
            'title' => _f('Syndication feed of %s', $collection->name),
        ];

        $current_tab = 'links';
    }

    if ($pagination->isCurrentFirstPage()) {
        $current_url_params = ['id' => $collection->id];
    } else {
        $current_url_params = [
            'id' => $collection->id,
            'page' => $pagination->currentPage(),
        ];
    }

    $this->layout('base.phtml', [
        'title' => $collection->name,
        'canonical' => url_full('collection', $current_url_params),
        'current_tab' => $current_tab,
        'alternates' => [$feed],
    ]);
?>

<div class="section">
    <?php if ($collection->image_filename): ?>
        <img
            class="section__image"
            alt=""
            src="<?= url_media('large', $collection->image_filename) ?>"
        />
    <?php endif; ?>

    <div class="section__title">
        <h1><?= protect($collection->name) ?></h1>
    </div>

    <?php if ($topics): ?>
        <p class="collection__topics">
            <?= implode('&nbsp;·&nbsp;', array_column($topics, 'label')) ?>
        </p>
    <?php endif; ?>

    <div class="section__meta">
        <div class="section__details">
            <p class="collection__owner">
                <span class="collection__public sticker"><?= $collection->is_public ? _('public') : _('private') ?></span>

                <?= _f('Published by %s', collection_publishers($collection, $current_user)) ?></li>
            </p>

            <p title="<?= _('Publication frequency') ?>">
                <?= icon('line-chart') ?>
                <?= format_publication_frequency($collection->publication_frequency_per_year) ?>
            </p>
        </div>

        <div class="section__actions">
            <details
                class="popup"
                data-controller="popup"
                data-action="toggle->popup#update click@window->popup#closeOnClickOutside keydown->popup#closeOnEscape"
            >
                <summary class="popup__opener">
                    <span class="button button--small">
                        <?= icon('menu') ?>
                        <?= _('Actions') ?>
                    </span>
                </summary>

                <?php $is_following = $current_user->isFollowing($collection->id); ?>
                <?php $is_shared = count($collection->shares()) > 0; ?>

                <nav class="popup__container popup__container--left" role="menu">
                    <button
                        class="popup__item popup__item--button"
                        data-controller="modal-opener"
                        data-action="modal-opener#fetch"
                        data-modal-opener-href-value="<?= url('collection shares', ['id' => $collection->id, 'from' => url('collection', $current_url_params)]) ?>"
                        aria-haspopup="dialog"
                        aria-controls="modal"
                        role="menuitem"
                    >
                        <?= icon('share') ?>
                        <?= _('Share') ?>
                    </button>

                    <div class="popup__separator"></div>

                    <?php if ($is_following): ?>
                        <button
                            class="popup__item popup__item--button"
                            data-controller="modal-opener"
                            data-action="modal-opener#fetch"
                            data-modal-opener-href-value="<?= url('edit collection filter', ['id' => $collection->id, 'from' => url('collection', $current_url_params)]) ?>"
                            aria-haspopup="dialog"
                            aria-controls="modal"
                            role="menuitem"
                        >
                            <?= icon('slider') ?>
                            <?= _('Adjust for the news') ?>
                        </button>
                    <?php endif; ?>

                    <button
                        class="popup__item popup__item--button"
                        data-controller="modal-opener"
                        data-action="modal-opener#fetch"
                        data-modal-opener-href-value="<?= url('edit collection', ['id' => $collection->id, 'from' => url('collection', $current_url_params)]) ?>"
                        aria-haspopup="dialog"
                        aria-controls="modal"
                        role="menuitem"
                    >
                        <?= icon('pencil') ?>
                        <?= _('Edit') ?>
                    </button>

                    <button
                        class="popup__item popup__item--button"
                        data-controller="modal-opener"
                        data-action="modal-opener#fetch"
                        data-modal-opener-href-value="<?= url('edit group collection', ['id' => $collection->id, 'from' => url('collection', $current_url_params)]) ?>"
                        aria-haspopup="dialog"
                        aria-controls="modal"
                        role="menuitem"
                    >
                        <?= icon('directory') ?>
                        <?= _('Put in a group') ?>
                    </button>

                    <button
                        class="popup__item popup__item--button"
                        data-controller="modal-opener"
                        data-action="modal-opener#fetch"
                        data-modal-opener-href-value="<?= url('edit image collection', ['id' => $collection->id, 'from' => url('collection', $current_url_params)]) ?>"
                        aria-haspopup="dialog"
                        aria-controls="modal"
                        role="menuitem"
                    >
                        <?= icon('image') ?>
                        <?= _('Change the illustration') ?>
                    </button>

                    <?php if ($is_following): ?>
                        <div class="popup__separator"></div>

                        <form method="post" action="<?= url('unfollow collection', ['id' => $collection->id]) ?>" role="menuitem">
                            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                            <input type="hidden" name="from" value="<?= url('collection', $current_url_params) ?>" />

                            <button class="popup__item popup__item--button">
                                <?= icon('feed-stop') ?>
                                <?= _('Unfollow') ?>
                            </button>
                        </form>
                    <?php elseif ($is_shared): ?>
                        <div class="popup__separator"></div>

                        <form method="post" action="<?= url('follow collection', ['id' => $collection->id]) ?>" role="menuitem">
                            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                            <input type="hidden" name="from" value="<?= url('collection', $current_url_params) ?>" />

                            <button class="popup__item popup__item--button">
                                <?= icon('feed') ?>
                                <?= _('Follow') ?>
                            </button>
                        </form>
                    <?php endif; ?>

                    <?php if ($collection->user_id === $current_user->id): ?>
                        <div class="popup__separator"></div>

                        <form
                            method="post"
                            action="<?= url('delete collection', ['id' => $collection->id]) ?>"
                            data-turbo-confirm="<?= _('Are you sure that you want to delete this collection? You may lose the links that are attached only to this collection.') ?>"
                        >
                            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                            <input type="hidden" name="from" value="<?= url('collection', $current_url_params) ?>" />

                            <button
                                type="submit"
                                class="popup__item popup__item--button"
                                role="menuitem"
                            >
                                <?= icon('trash') ?>
                                <?= _('Delete') ?>
                            </button>
                        </form>
                    <?php endif; ?>
                </nav>
            </details>
        </div>
    </div>

    <?php if ($collection->description): ?>
        <div class="collection__description">
            <?= $collection->descriptionAsHtml() ?>
        </div>
    <?php endif; ?>

    <?php if ($pagination->numberElements() > 0): ?>
        <div class="collection__number-links">
            <?= _nf('%s link', '%s links', $pagination->numberElements(), format_number($pagination->numberElements())) ?>
        </div>
    <?php endif; ?>

    <div class="cards">
        <div class="card">
            <button
                class="card__button card__button--link"
                data-controller="modal-opener"
                data-action="modal-opener#fetch"
                data-modal-opener-href-value="<?= url('new collection link', ['id' => $collection->id, 'from' => url('collection', $current_url_params)]) ?>"
                aria-haspopup="dialog"
                aria-controls="modal"
            >
                <?= icon('plus-circle') ?><br />
                <?= _('Add a link') ?>
            </button>
        </div>

        <?php foreach ($links as $link): ?>
            <?= $this->include('links/_link.phtml', [
                'link' => $link,
                'from' => \Minz\Url::for('collection', $current_url_params),
                'display_source' => $link->source_type !== 'bookmarks',
                'display_notes' => true,
                'display_hidden' => $collection->is_public && $link->is_hidden,
                'display_edit' => $link->user_id === $current_user->id,
                'display_repair' => $link->user_id === $current_user->id,
                'display_delete' => $link->user_id === $current_user->id,
                'display_read_later' => 'auto',
                'display_mark_as_read' => 'auto',
            ]); ?>
        <?php endforeach; ?>

        <?php for ($i = 0; $i < (3 - ((count($links) + 1) % 3)); $i++): ?>
            <div class="card card--shadow no-mobile"></div>
        <?php endfor; ?>
    </div>

    <?= $this->include('_pagination.phtml', ['pagination' => $pagination, 'url' => url('collection', ['id' => $collection->id])]) ?>
</div>
